<script setup>
import { defineEmits, defineProps } from 'vue';

// 定义props
const props = defineProps({
    isPaused: {
        type: Boolean,
        default: false
    },
    gameOver: {
        type: Boolean,
        default: false
    },
    gameWon: {
        type: Boolean,
        default: false
    },
    resultMessage: {
        type: String,
        default: ''
    },
    showExitConfirm: {
        type: Boolean,
        default: false
    },
    remainingTime: {
        type: Number,
        default: 0
    },
    totalAiCount: {
        type: Number,
        default: 0
    },
    aliveAiCount: {
        type: Number,
        default: 0
    }
});

// 定义事件
const emit = defineEmits([
    'pause', 
    'resume',
    'restart',
    'exit',
    'simulateCompletion',
    'closeExitConfirm'
]);

// 暂停/恢复游戏
const togglePause = () => {
    if (props.isPaused) {
        emit('resume');
    } else {
        emit('pause');
    }
};

// 模拟通关
const simulateCompletion = () => {
    emit('simulateCompletion');
};

// 格式化时间显示
const formatTime = (seconds) => {
    const minutes = Math.floor(seconds / 60);
    const remainingSeconds = seconds % 60;
    return `${minutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
};
</script>

<template>
    <!-- 游戏控制界面 -->
    <div class="game-controls" v-if="!gameOver">
        <div class="info-item hint">
            <i class="fas fa-info-circle"></i>
            <span>按Q键可以使用火球技能，左键发射，右键移动</span>
        </div>
        
        <div class="control-buttons">
            <!-- 暂停按钮 -->
            <button @click="togglePause" class="control-button pause-button">
                <i :class="isPaused ? 'fas fa-play' : 'fas fa-pause'"></i>
            </button>
            
            <!-- 退出按钮 -->
            <button @click="$emit('closeExitConfirm', true)" class="control-button exit-button">
                <i class="fas fa-sign-out-alt"></i>
            </button>
            
            <!-- 测试用的通关按钮 -->
            <button @click="simulateCompletion" class="complete-button">
                测试通关
            </button>
        </div>
    </div>

    <!-- 时间计数器 -->
    <div v-if="!gameOver && remainingTime >= 0" class="time-counter" :class="{ 'danger': remainingTime <= 10 }">
        倒计时: {{ formatTime(remainingTime) }}
    </div>

    <!-- AI计数显示 -->
    <div v-if="!gameOver && totalAiCount > 0" class="ai-counter">
        存活的AI: {{ aliveAiCount }}/{{ totalAiCount }}
    </div>
    
    <!-- 暂停菜单 -->
    <div v-if="isPaused && !gameOver" class="pause-menu">
        <div class="pause-menu-content">
            <h2>游戏已暂停</h2>
            <div class="pause-buttons">
                <button @click="$emit('resume')" class="menu-button resume-button">
                    <i class="fas fa-play"></i> 继续游戏
                </button>
                <button @click="$emit('restart')" class="menu-button restart-button">
                    <i class="fas fa-redo"></i> 重新开始
                </button>
                <button @click="$emit('closeExitConfirm', true)" class="menu-button exit-button">
                    <i class="fas fa-sign-out-alt"></i> 退出游戏
                </button>
            </div>
        </div>
    </div>
    
    <!-- 退出确认弹窗 -->
    <div v-if="showExitConfirm" class="exit-confirm-overlay">
        <div class="exit-confirm-modal">
            <h3>确定要退出游戏吗？</h3>
            <p>当前游戏进度将不会保存</p>
            <div class="confirm-buttons">
                <button @click="$emit('exit')" class="confirm-button confirm-yes">
                    确定
                </button>
                <button @click="$emit('closeExitConfirm', false)" class="confirm-button confirm-no">
                    取消
                </button>
            </div>
        </div>
    </div>

    <!-- 游戏结果弹窗 -->
    <div v-if="gameOver" class="game-result">
        <div class="result-content" :class="{ 'win': gameWon, 'lose': !gameWon }">
            <h2>{{ gameWon ? '胜利！' : '失败！' }}</h2>
            <p>{{ resultMessage }}</p>
            <button @click="$emit('restart')" class="restart-btn">重新开始</button>
            <button @click="$emit('exit')" class="exit-btn">退出游戏</button>
        </div>
    </div>
</template>

<style scoped>
/* 游戏控制界面样式 */
.game-controls {
    position: absolute;
    top: 20px;
    right: 20px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding: 10px 15px;
    background: rgba(0, 0, 0, 0.7);
    border-radius: 8px;
    z-index: 10;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(0, 255, 136, 0.2);
}

.info-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: #ddd;
}

.info-item i {
    color: #00ff88;
}

.info-item.hint {
    background: rgba(0, 255, 136, 0.1);
    padding: 0.3rem 0.8rem;
    border-radius: 4px;
    font-size: 0.9rem;
    margin-right: 10px;
}

.control-buttons {
    display: flex;
    gap: 0.5rem;
}

.control-button {
    background: rgba(0, 0, 0, 0.5);
    color: #00ff88;
    border: 1px solid rgba(0, 255, 136, 0.3);
    padding: 0.5rem;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.control-button:hover {
    transform: scale(1.05);
    box-shadow: 0 0 10px rgba(0, 255, 136, 0.5);
    background: rgba(0, 255, 136, 0.2);
}

.pause-button i, .exit-button i {
    font-size: 1rem;
}

.complete-button {
    background: linear-gradient(90deg, #00ff88, #00ccff);
    color: #000;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    transition: all 0.3s ease;
}

.complete-button:hover {
    transform: scale(1.05);
    box-shadow: 0 0 10px rgba(0, 255, 136, 0.5);
}

/* 时间计数器 */
.time-counter {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background-color: rgba(0, 0, 0, 0.7);
    color: #00ff88;
    font-size: 24px;
    padding: 10px 20px;
    border-radius: 10px;
    z-index: 10;
    box-shadow: 0 0 15px rgba(0, 255, 136, 0.3);
    border: 1px solid rgba(0, 255, 136, 0.5);
    font-family: 'Courier New', monospace;
    font-weight: bold;
    transition: color 0.3s ease;
}

.time-counter.danger {
    color: #ff4444;
    animation: pulse 1s infinite;
    border-color: rgba(255, 68, 68, 0.7);
    box-shadow: 0 0 15px rgba(255, 68, 68, 0.5);
}

/* AI计数器 */
.ai-counter {
    position: absolute;
    top: 20px;
    left: 20px;
    background: rgba(0, 0, 0, 0.7);
    padding: 10px 15px;
    border-radius: 8px;
    border: 1px solid rgba(0, 255, 136, 0.4);
    color: #00ff88;
    font-size: 16px;
    font-weight: bold;
    z-index: 10;
    box-shadow: 0 0 10px rgba(0, 255, 136, 0.3);
}

/* 暂停菜单样式 */
.pause-menu {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    backdrop-filter: blur(5px);
}

.pause-menu-content {
    background: #1a1a1a;
    border-radius: 12px;
    width: 90%;
    max-width: 500px;
    padding: 2rem;
    text-align: center;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    border: 2px solid rgba(0, 255, 136, 0.3);
    animation: fadeIn 0.3s ease;
}

.pause-menu-content h2 {
    font-size: 2rem;
    color: #00ff88;
    margin-bottom: 1.5rem;
    text-shadow: 0 0 10px rgba(0, 255, 136, 0.5);
}

.pause-buttons {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-top: 2rem;
}

.menu-button {
    background: rgba(0, 255, 136, 0.1);
    color: #00ff88;
    border: 1px solid rgba(0, 255, 136, 0.3);
    padding: 0.8rem 2rem;
    border-radius: 6px;
    font-size: 1.2rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.menu-button:hover {
    transform: scale(1.02);
    background: rgba(0, 255, 136, 0.2);
    box-shadow: 0 0 15px rgba(0, 255, 136, 0.5);
}

.menu-button.exit-button {
    background: rgba(255, 77, 77, 0.1);
    color: #ff4d4d;
    border: 1px solid rgba(255, 77, 77, 0.3);
}

.menu-button.exit-button:hover {
    background: rgba(255, 77, 77, 0.2);
    box-shadow: 0 0 15px rgba(255, 77, 77, 0.5);
}

/* 退出确认弹窗样式 */
.exit-confirm-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 110;
    backdrop-filter: blur(5px);
}

.exit-confirm-modal {
    background: #1a1a1a;
    border-radius: 12px;
    width: 90%;
    max-width: 400px;
    padding: 2rem;
    text-align: center;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    border: 2px solid rgba(255, 77, 77, 0.3);
    animation: fadeIn 0.3s ease;
}

.exit-confirm-modal h3 {
    font-size: 1.8rem;
    margin-bottom: 1rem;
    color: #ff4d4d;
    text-shadow: 0 0 10px rgba(255, 77, 77, 0.5);
}

.exit-confirm-modal p {
    margin-bottom: 2rem;
    font-size: 1.2rem;
    color: #ddd;
}

.confirm-buttons {
    display: flex;
    gap: 1rem;
    justify-content: center;
}

.confirm-button {
    padding: 0.8rem 2rem;
    border-radius: 6px;
    font-size: 1.1rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s ease;
    width: 120px;
}

.confirm-button:hover {
    transform: scale(1.05);
}

.confirm-button.confirm-yes {
    background: rgba(255, 77, 77, 0.1);
    color: #ff4d4d;
    border: 1px solid rgba(255, 77, 77, 0.3);
}

.confirm-button.confirm-yes:hover {
    background: rgba(255, 77, 77, 0.2);
    box-shadow: 0 0 15px rgba(255, 77, 77, 0.5);
}

.confirm-button.confirm-no {
    background: rgba(0, 255, 136, 0.1);
    color: #00ff88;
    border: 1px solid rgba(0, 255, 136, 0.3);
}

.confirm-button.confirm-no:hover {
    background: rgba(0, 255, 136, 0.2);
    box-shadow: 0 0 15px rgba(0, 255, 136, 0.5);
}

/* 游戏结果样式 */
.game-result {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.result-content {
    background-color: #1a1a1a;
    border-radius: 15px;
    padding: 30px;
    width: 400px;
    max-width: 90%;
    text-align: center;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
}

.result-content.win {
    border: 2px solid #00ff88;
}

.result-content.lose {
    border: 2px solid #ff4444;
}

.result-content h2 {
    font-size: 32px;
    margin-bottom: 15px;
}

.win h2 {
    color: #00ff88;
}

.lose h2 {
    color: #ff4444;
}

.result-content p {
    font-size: 18px;
    margin-bottom: 30px;
    color: #ddd;
}

.restart-btn, .exit-btn {
    padding: 10px 25px;
    border-radius: 8px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    margin: 0 10px;
    transition: all 0.3s ease;
}

.restart-btn {
    background: linear-gradient(90deg, #00ff88, #00ccff);
    color: #000;
    border: none;
}

.restart-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 0 15px rgba(0, 255, 136, 0.5);
}

.exit-btn {
    background: rgba(255, 255, 255, 0.1);
    color: #ddd;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.exit-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes pulse {
	0% {
		text-shadow: 0 0 5px rgba(255, 68, 68, 0.7);
	}
	50% {
		text-shadow: 0 0 20px rgba(255, 68, 68, 1);
	}
	100% {
		text-shadow: 0 0 5px rgba(255, 68, 68, 0.7);
	}
}
</style>
